package Engine.AIUGame
{
	import Engine.AIUGame.AIGDate;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.utils.getTimer;
	
	public class AIGame extends Sprite
	{
		
		private var _frameRate:uint 		//每秒播放的帧数
		private var _frameRateFlash:uint; //每秒flash player播放的帧
		private var _frameRateStep:uint;	//每播一帧需要多少毫秒
		
		private var _maxAccumulation:uint  //最大延时帧率
		
		private var _lastFrameTime:uint;		//上一帧的时间
		private var _accumulator:uint		//
		private var _lostFocus:Boolean;
		
		protected var _movieGroup:AIGroup;
		
		public function AIGame(GameSizeX:uint,GameSizeY:uint,GameFrameRate:uint = 60, GameFrameRateFlash:uint = 30)
		{
			super();
			
			_lastFrameTime = 0;
			_lostFocus = true;
			
			frameRate = GameFrameRate;
			framteRateFlash = GameFrameRateFlash;
			
			_accumulator = _frameRateStep;
			
			addEventListener(Event.ADDED_TO_STAGE,onAddToState)
			addEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromState);
			
			_movieGroup = new AIGroup();
			
			AIGDate.init(this,GameSizeX,GameSizeY);
			AIGDate.reset();
		}
		
		private function onAddToState(e:Event):void
		{
			_lastFrameTime = getTimer();
			_lostFocus = false;
			stage.frameRate = _frameRateFlash;
			AIGDate.addCameraView();
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}
		
		private function onRemoveFromState(e:Event):void
		{
			_lostFocus = true;
			AIGDate.removeCameraView();
			removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.removeEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}
		
		private function onEnterFrame(e:Event):void
		{
			var mark:uint = getTimer();
			var elapsedMS:uint = mark-_lastFrameTime;
			_lastFrameTime = mark;
			if(!_lostFocus)
			{
				_accumulator += elapsedMS;
				if(_accumulator > _maxAccumulation)
					_accumulator = _maxAccumulation;
				while(_accumulator >= _frameRateStep)
				{
					update();
					_accumulator = _accumulator - _frameRateStep; 
				}
				draw();
			}
		}
		
		private function set frameRate(value:uint):void
		{
			_frameRate = value
			_frameRateStep = 1000/_frameRate;
			if(_maxAccumulation < _frameRateStep)
				_maxAccumulation = _frameRateStep;
		}
		
		private function set framteRateFlash(value:uint):void
		{
			_frameRateFlash = value;
			_maxAccumulation = 2000/_frameRateFlash - 1;
			if(_maxAccumulation < _frameRateStep)
				_maxAccumulation = _frameRateStep;
		}
		
		//游戏主帧率调用接口
		protected function update():void
		{
			
			AIGDate.elapsed = AIGDate.timeScale*(_frameRateStep/1000);
			AIGDate.keys.update();
			_movieGroup.update();
			AIGDate.updateViewCamera();
		}
		
		//游戏渲染
		private function draw():void
		{
			AIGDate.GameView.fill(0x00000000);
			_movieGroup.draw();
		}
		
		//返回当前游戏正在渲染的场景
		public function get gameGroup():AIGroup
		{
			return _movieGroup;
		}
		
		private function onKeyDown(event:KeyboardEvent):void
		{
			AIGDate.keys.handleKeyDown(event);
		}
		
		private function onKeyUp(event:KeyboardEvent):void
		{
			AIGDate.keys.handleKeyUp(event);
		}
		
	}
}